package com.uvlab.sitespot.service.member;

import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/6/17 0017.
 */
public class JxMemberService {
    private LinkedHashMap items =new LinkedHashMap();
    RestTemplate restTemplate;
    private  String url="http://localhost:8080";
    public JxMemberService(){

    }

    public void init(String url){
        this.url=url;
    }

    public  LinkedHashMap getMemberList(){
        restTemplate=new RestTemplate();
        return  restTemplate.getForObject(url+"/member",LinkedHashMap.class);
    }
    public  LinkedHashMap getMemberList(HashMap<String,String> params){
        StringBuilder sb=new StringBuilder(); //每次追加一个&+参数，最后将第一个&修改为？
        sb.append(url).append("/member");
        for (Object o : params.entrySet()) {
            Map.Entry entry = (Map.Entry) o;
            String key = (String) entry.getKey();
            String val = (String) entry.getValue();
            if (val != null) {
                String tmp = String.format("&%s=%s", key, val);
                sb.append(tmp);
            }
        }
        int x=sb.indexOf("&");
        if(x!=-1) sb.replace(x,x+1,"?");
        String urlx=sb.toString();
        restTemplate=new RestTemplate();
        return  restTemplate.getForObject(urlx,LinkedHashMap.class);
    }

    public  void addMember(JxMember mem){
        restTemplate=new RestTemplate();
        restTemplate.postForObject(url+"/member",mem,String.class);
    }

    public  void updateMember(JxMember men){
        restTemplate=new RestTemplate();
        restTemplate.put(url+"/member",men);
    }

    public  void deleteMember(String id){
        restTemplate=new RestTemplate();
        restTemplate.delete(url+"/member/"+id);
    }

    public JxMember getById(String id){
        restTemplate=new RestTemplate();
        return  restTemplate.getForObject(url+"/member/"+id,JxMember.class);
    }
}
